package org.to_zero.ssm.sdm.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.to_zero.ssm.sdm.entity.User;
import org.to_zero.ssm.sdm.entity.vo.ResponseEntity;
import org.to_zero.ssm.sdm.service.AdminService;
import org.to_zero.ssm.sdm.util.Toolkit;

import javax.servlet.http.HttpServletRequest;
import java.util.logging.Logger;

@RequestMapping("/back/admin/")
@RestController
public class AdminController {
    @Autowired
    private AdminService adminService;
    private Logger log = Logger.getLogger("admin");

    @GetMapping("getAdmin")
    public ResponseEntity getAdmin(HttpServletRequest request) {
        String token = Toolkit.getToken(request);
        return adminService.getAdmin(token);
    }

    // 登录
    @GetMapping("login")
    public ResponseEntity login(String username, String password, String verifyCode, boolean isRememberMe) {
        log.info("{username}:" + username + " {password}:" + password + "{verifyCode}:" + verifyCode + " {isRememberMe}:" + isRememberMe);
        return adminService.login(username, password, verifyCode, isRememberMe);
    }

    // 记住我
    @GetMapping("rememberMe")
    public ResponseEntity rememberMe() {
        return adminService.rememberMe();
    }

    //注册
    @PostMapping("register")
    public ResponseEntity register(@RequestBody User user) {
        return adminService.register(user);
    }

    //登出
    @DeleteMapping("logout")
    public ResponseEntity logout(HttpServletRequest request) {
        String token = Toolkit.getToken(request);
        return adminService.logout(token);
    }

    @GetMapping("changePassword")
    public ResponseEntity changePassword(Integer id, String currentPassword, String password) {
        return adminService.changePassword(id, currentPassword, password);
    }

    @PostMapping("changeProfile")
    public ResponseEntity changeProfile(@RequestBody User user, HttpServletRequest request) {
        return adminService.changeProfile(user, request);
    }
}
